n=0
A=[]
B=[]
for a in range(1,7):
    for b in range(1,7):
        for c in range(1,7):
            m=a+b+c
            if m == 6:
                A.append(a)
                A.append(b)
                A.append(c)
                B.append(A)
                A=[]
                    
print B

a=[1,2,3,4,5,6]
def combination(n,k):
    a=map(lambda x:x+1,range(n))
    b=[]
    p=0
    E=[]
    if k==1:
        for i in range(len(a)-1):
            b.append(a[i])
            E.append(b)
            b=[]
    else:       
        while 1:
            c=a[p]
            b=[c]
            while b:
                for i in a[a.index(c)+1:]:
                    if i not in b:
                        b.append(i)
                        if len(b)>k-1:
                            E.append(b[:])
                            break
                c=b.pop()
            p=p+1
            if p==n:
                break
    return E
def GetFirstCmn(m,n):
    return range(1,n+1)
def GetNextCmn(m,n,c):
    cmn = combination(m,n)
    cid = cmn.index(c)
    if c in cmn and cid<len(cmn)-1:
        return cmn[cid+1]
    else:
        return None
'''
M,N=5,3
S=[]
c=GetFirstCmn(M,N)
while c:
    print c
    c=GetNextCmn(M,N,c)
'''    
t=0
def repeat(p,a,n):
    if n==1:
        t=a[a.index(p[j][0])]
        del a[a.index(p[j][0])]
    else:
        for k in range(n-1):
            t=a[a.index(p[j][k])]
            del a[a.index(p[j][k])]
    return a,t
def rep2(a,p):
 	for i in p:
 		del a[a.index(i)]

def GetCAFromA(a,p):
    r=[]
    p.sort(reverse=True)
    for i in p:
        r.append(a[i-1])#a.index(i)]
        del a[i-1]
    return r
def SumLen(a):#return all item length sum
    s=0
    for i in a:
        s+=len(i)
    return s
A=a[:]
B=[1,2,3]#,[1,2,3]]
S=[]
T=[]
S.append(B[0])
ML=len(a)
BId=0
while 1:
    FF=[]
    while 1:
        while BId<3:
          ca = GetFirstCmn(len(A),B[BId])
          ra = tuple(GetCAFromA(A,ca))
          FF.append(ra)
          if SumLen(FF)==ML:
            print FF
            A+=ra
            A.sort()
            ca.sort()
            break
          BId+=1
        ca=GetNextCmn(len(A),B[BId],ca)
        if ca is None:
            del FF[-1]
            BId-=1
            A+=FF[-1]
            A.sort()
            
            
        

C=[]
D=[]
S1=[]
S2=[]
S1.append(m[0])
while S1:
    p=combination(len(a),S1[-1])
    for j in range(len(p)-1):
        S.push(p[j])
        rep2(a,p[j])
        C.append(p[j])
        
    continue
